<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <script src="../js/animate.js"></script>
    <script src="../font/iconfont.js"></script>
    <style>
        :root {
            --cool: #40C5F1;
            --hot: #f00;
            --grey: #423029;
            --light: #C3D0DF;
        }

        .wrap {
            width: 1226px;
            height: 460px;
            margin: 50px auto;
            position: relative;
            overflow: hidden;
        }

        .nav {
            display: flex;
            line-height: 40px;
            text-align: center;
            position: absolute;
            right: 20px;
            bottom: 20px;
            z-index: 99999;
        }

        .nav li {
            width: 15px;
            height: 15px;
            border-radius: 50%;
            margin: 0 5px;
            flex: 1;
            background-color: var(--light);
            /* border: 1px solid var(--grey); */

        }

        .nav .active {
            background-color: var(--cool);
            color: #fff;
        }

        .slide-wrap {
            width: 500%;
            overflow: hidden;
            position: relative;
            left: 0;
        }

        .slide-wrap .slide-item {
            width: 20%;
            float: left;
        }

        .slide-wrap .slide-item img {
            width: 100%;
        }
        .icon {
            width: 1em;
            height: 1em;
            vertical-align: -0.15em;
            fill: currentColor;
            overflow: hidden;
        }
        .wrap .prev,
        .wrap .next{
            width: 50px;
            height: 50px;
            /* background-color: rgba(0,0,0,0.5); */
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            z-index: 99;
            display: none;
        }

        .wrap .prev .icon,
        .wrap .next .icon{
            width: 100%;
            height: 100%;
        }

        .wrap .prev{
            left: 10px;
        }

        .wrap .next{
            right: 10px;
        }

    </style>
</head>

<body>
    <div class="wrap">
        <div class="prev">
            <svg class="icon" aria-hidden="true">
                <use xlink:href="#icon-zuo1"></use>
            </svg>
        </div>
        <div class="next">
            <svg class="icon" aria-hidden="true">
                <use xlink:href="#icon-you2"></use>
            </svg>
        </div>
        <ul class="nav">
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <div class="slide-wrap">
            <div class="slide-item"><a href="https://www.baidu.com"><img src="../images/1.jpg" alt=""></a></div>
            <div class="slide-item"><a href="https://www.jd.com"><img src="../images/2.webp" alt=""></a></div>
            <div class="slide-item"><a href="https://www.taobao.com"><img src="../images/3.webp" alt=""></a></div>
            <div class="slide-item"><a href="https://www.bilibili.com"><img src="../images/4.webp" alt=""></a></div>
            <div class="slide-item"><a href="https://www.bilibili.com"><img src="../images/1.jpg" alt=""></a></div>
        </div>
    </div>
</body>
<script>
    // 点击每个li 打印li本身及其下标

    var wrap = document.getElementsByClassName("wrap")[0];
    var nav = document.getElementsByClassName("nav")[0];
    var slideWrap = document.getElementsByClassName("slide-wrap")[0];
    var liList = nav.getElementsByTagName("li");
    var listItems = document.querySelectorAll(".list-wrap .list-item");
    var prev = document.getElementsByClassName("prev")[0];
    var next = document.getElementsByClassName("next")[0];

    
    var slideWidth = wrap.clientWidth;
    var timer = null;
    var index = 0;  // 全局变量记录轮播的下标

    // 页面加载时  => 启动轮播
    autoPlay();

    // 点击切换 
    // 1. 点 和 图片一一对应
    // 2. 点的下标  和 向左滚动的图片的张数 也是对应的

    for (let i = 0; i < liList.length; i++) {
        let li = liList[i];
        li.onclick = function () {
            console.log(i);

            index = i;

            for (var j = 0; j < liList.length; j++) {
                liList[j].className = "";
            }
            liList[i].className = "active";

            animate(slideWrap, "left", -slideWidth * i, "ease-out", function () {
                console.log("本次运动完毕");
            })
        }
    }

    wrap.onmouseenter = function () {
        clearInterval(timer);
        prev.style.display = "block";
        next.style.display = "block";
    }
    wrap.onmouseleave = function () {
        prev.style.display = "none";
        next.style.display = "none";
        autoPlay();
    }

    prev.onclick = function(){
        index --;
        step();
    }

    next.onclick = function(){
        index ++;
        step();
    }


    function autoPlay() {
        clearInterval(timer);
        timer = setInterval(function () {
            index++; // 0 1 2 3 4
            
            step();
        }, 3000);
    }



    function step(){

        // 左键连点 => 左键下标最小值为0(第一张) => 如何让其平滑滚动到第四张 ?
        // 第一张  瞬间切到第五张之后   滚动到  第四张
        if(index < 0){ // 如何让第一张平滑滚动到第四张 ?
            index = liList.length;  // 第五张下标
            slideWrap.style.left = - slideWidth * index + "px";  // 瞬间切到第五张之后 
            index --;  // 滚动到  第四张
        }
      

        // next连续点击 => animate函数重复触发 => 先清除上一个,在启动当前 => 回调函数不会触发
        // => 连续点击 => 临界判断优化处理

        // 解析: next连续点击 => 图片最多只能滚动到第五张(看起来同第一张 -> 临界值) -> 此时再点击应该滚动到第二张 => 如何滚动第二张

        // 超出第五张瞬间切换到第一张   滚动到  第二张

        if(index > liList.length){ // index > 4 => 超出第五张
            slideWrap.style.left = 0;  // 第五张瞬间切换到第一张
            index = 0;                 // 第一张对应下标
            index++;                   // 滚动到第二张
        }   
        

        for (var j = 0; j < liList.length; j++) {
            liList[j].className = "";
        }
        var activeIndex = index >= liList.length ? 0 : index;
        liList[activeIndex].className = "active";

        animate(slideWrap, "left", -slideWidth * index, "ease-out", function () {
            // console.log("本次运动完毕");
            // // 回调函数 =>  每次运动完毕 判断是否是第五张对应的下标
            if(index >= liList.length){
                slideWrap.style.left = 0;
                index = 0;
            }
        })
    }









</script>

</html>